package com.xkdx.guangguang.fragment.city;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.xkdx.caipiao.HomePageActivity;
import com.xkdx.caipiao.R;
import com.xkdx.guangguang.ManagerActivity;
import com.xkdx.guangguang.fragment.BaseFragment;
import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.module.statics.IConstants;
import com.xkdx.guangguang.util.SharePrefenceUtil;

@SuppressLint("ParserError")
public class CityFragment extends BaseFragment {
	View view;
	ListView list;
	Button back;
	List<Map<String, String>> cityList;
	ProgressBar pb;
	SharePrefenceUtil su;
	LayoutInflater inflater;
//	HomeModule homeModule;
	CityAction cityAction;
	CityModule cityModule;
	CityPresistence cityPresistence;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		su = new SharePrefenceUtil(getActivity(), IConstants.SP_CITY);
		inflater = LayoutInflater.from(getActivity());

//		homeModule = ((HomeActivity) getActivity()).getHomeModule();
//		if (homeModule != null && homeModule.cityList != null) {
//			cityList = homeModule.cityList;
//		}
		
		if(HomePageActivity.cityList != null){
			cityList = HomePageActivity.cityList;
		}

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.city, container, false);
		findView();
		setOnClick();
		if (cityList == null) {
			showLoading();
		} else {
			setCity();
		}

		return view;
	}

	@Override
	public void setOnClick() {

		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				getActivity().finish();

			}
		});
	}

	@Override
	public void showLoaded(HashMap<String, AbsModule> msg) {
		pb.setVisibility(View.GONE);
		if (msg.containsKey("0")) {
			cityList = cityModule.cityList;
			setCity();
		} else {
			refresh(msg);
		}
	}

	private void setCity() {

		MyAdapter adapter = new MyAdapter();
		list.setAdapter(adapter);
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

				String LocationCity = su.getLocationCityName();

				String choiceCity = cityList.get(position).get("CityName");
				if (!LocationCity.equals(choiceCity.substring(0, 2))) {
					showTip(LocationCity, choiceCity, position);
				} else {
					// ShopFragment.list = null;
					su.setCityID(cityList.get(position).get("CityID"));
					su.setChoiceCityName(cityList.get(position).get("CityName"));
                    IConstants.isChoiceCity = true;
					getActivity().finish();
				}

			}

			private void showTip(String currentCity, String choiceCity, final int position) {
				AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()).setTitle("提示");
				builder.setMessage("所选城市" + choiceCity + ",不是所在城市" + currentCity + ",确认选择？");
				builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// 清空商场首页列表信息
						su.setCityID(cityList.get(position).get("CityID"));
						su.setChoiceCityName(cityList.get(position).get("CityName"));
                        IConstants.isChoiceCity = true;
						dialog.dismiss();
						getActivity().finish();
					}
				});
				builder.setNegativeButton("取消", null);
				builder.show();
			}

		});

	}

	class MyAdapter extends BaseAdapter {

		public MyAdapter() {
			super();
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return cityList.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return cityList.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			if (convertView == null) {
				convertView = inflater.inflate(R.layout.city_item, null);
			}

			TextView tv = (TextView) convertView.findViewById(R.id.city);

			tv.setText(cityList.get(position).get("CityName"));

			return convertView;
		}

	}

	@Override
	public void findView() {
		list = (ListView) view.findViewById(R.id.city_list);
		back = (Button) view.findViewById(R.id.back_city);
		pb = (ProgressBar) view.findViewById(R.id.city_progressbar);
		cityAction = new CityAction();
		cityModule = new CityModule();
	}

	@Override
	public void showLoading() {
		pb.setVisibility(View.VISIBLE);
		cityPresistence = new CityPresistence(this);
		((ManagerActivity) getActivity()).addCurrentTask(cityPresistence);
		cityPresistence.setActitons(cityAction);
		cityPresistence.setModule(cityModule);
		cityPresistence.execute();
	}

	@Override
	public void showClick(HashMap<String, AbsModule> map) {
		// TODO Auto-generated method stub

	}

	@Override
	public void showLoadMore(HashMap<String, AbsModule> map) {
		// TODO Auto-generated method stub

	}

}
